package com.xzzz.irda.guardcat.core.metric.classloading;

import java.io.Serializable;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;

/**
 * 类加载信息
 *
 * @author xzzz
 * @since 1.0.0
 */
public class ClassLoadingInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 已加载的类的总数量
     */
    private long totalLoadedClassCount;

    /**
     * 当前加载的类的数量
     */
    private int loadedClassCount;

    /**
     * 已卸载类的数量
     */
    private long unloadedClassCount;

    /**
     * 构造器私有化
     */
    private ClassLoadingInfo() {
    }

    /**
     * 获取类加载信息
     *
     * @return ClassLoadingInfo
     */
    public static ClassLoadingInfo getClassLoadingInfo() {
        ClassLoadingInfo classLoadingInfo = new ClassLoadingInfo();
        ClassLoadingMXBean classLoadingMxBean = ManagementFactory.getClassLoadingMXBean();
        classLoadingInfo.totalLoadedClassCount = classLoadingMxBean.getTotalLoadedClassCount();
        classLoadingInfo.loadedClassCount = classLoadingMxBean.getLoadedClassCount();
        classLoadingInfo.unloadedClassCount = classLoadingInfo.getUnloadedClassCount();
        return classLoadingInfo;
    }

    public long getTotalLoadedClassCount() {
        return totalLoadedClassCount;
    }

    public int getLoadedClassCount() {
        return loadedClassCount;
    }

    public long getUnloadedClassCount() {
        return unloadedClassCount;
    }

    @Override
    public String toString() {
        return "类加载信息{" +
                "已加载的类的总数量=" + totalLoadedClassCount +
                ", 当前加载的类的数量=" + loadedClassCount +
                ", 已卸载类的数量=" + unloadedClassCount +
                '}';
    }
}
